package org.hu.vhr.service;

import org.hu.vhr.mapper.MenuRoleMapper;
import org.hu.vhr.mapper.RoleMapper;
import org.hu.vhr.model.RespBean;
import org.hu.vhr.model.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoleService  {
    @Autowired
    RoleMapper roleMapper;

    @Autowired
    MenuRoleMapper menuRoleMapper;

    public List<Role> getAllRoles() {
        return roleMapper.getAllRoles();
    }

    public Integer insertRoles(Role role) {
        System.out.println(role.getName());
        System.out.println(role.getNameZh());
        if(!role.getName().startsWith("ROLE_")){
            role.setName("ROLE_"+role.getName());
        }
        return roleMapper.insertSelective(role);
    }


    public Integer updateRoles(Role role) {
        return roleMapper.updateByPrimaryKeySelective(role);
    }

    @Transactional
    public int deleteRole(Integer id) {
        menuRoleMapper.deleteByRid(id);
        Integer result = roleMapper.deleteByPrimaryKey(id);
        return result;
    }
}
